import 'package:intl/intl.dart';

/// 解析日期选择器
String parseDatePickerSelected(Map<String, int> selected) {
  final String year = selected['year'].toString().padLeft(4, '0');
  final String month = selected['month'].toString().padLeft(2, '0');
  final String day = selected['day'].toString().padLeft(2, '0');

  return '$year-$month-$day';
}

/// 格式化时间
String formatDateTime(DateTime dateTime, [String format = 'yyyy-MM-dd']) {
  final DateFormat formatter = DateFormat(format);
  // return formatter.format(dateTime);
  return Intl.withLocale('zh_CN', () => formatter.format(dateTime));
}

/// 通用列表转换函数
List<T> convertListResponse<T>(dynamic data, T Function(Map<String, dynamic>) fromJson) {
  if (data is List) {
    return data.map((item) => fromJson(item as Map<String, dynamic>)).toList();
  }
  throw FormatException('Expected a list of items for conversion, but got ${data.runtimeType}');
}